package com.jiamao.goplay.ui.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;

import com.discussionavatarview.DensityUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.NearbyAdapter;
import com.jiamao.goplay.adapter.UserUpdateAhAdapter;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
//import com.jiamao.goplay.databinding.DiscoverCityGotoLayoutBinding;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.databinding.UserUpdateAhLayoutBinding;
import com.jiamao.goplay.ui.discover.vm.CityGoToVM;
import com.jiamao.goplay.ui.user.vm.UpdateAHVM;
import com.jiamao.goplay.ui.user.vm.UserUpdateAHItemVM;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.tamsiree.rxkit.view.RxToast;

public class UserUpdateAhFragment extends BaseMVVMFragment<UserUpdateAhLayoutBinding, UpdateAHVM> {


    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public UpdateAHVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UpdateAHVM.class);
    }
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.user_update_ah_layout;
    }

    @Override
    public void initViewObservable() {

        viewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                Log.d("html","aaaaaaaaaaa"+aBoolean +" iit "+aBoolean);
              //  binding.getRoot().findViewById(R.id.ll_menu).setEnabled(aBoolean);
                if (aBoolean){

                }else {

                }
            }
        });

        super.initViewObservable();


    }

    @Override
    public void initData() {
        super.initData();
        UserUpdateAhAdapter adapter = new UserUpdateAhAdapter(getActivity());
        binding.setAdapter(adapter);

     //   ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();

        //binding.inputName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLen)});//20
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).statusBarView(binding.getRoot().findViewById(R.id.status_bar_view)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tv_center_title);
        titleTv.setText("添加兴趣");
        binding.getRoot().findViewById(R.id.tv_left_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        binding.getRoot().findViewById(R.id.ll_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                if (viewModel.num < 3){
                    KLog.d("最少选择3个规格嗯嗯");

                    RxToast.error("最少选择3个兴趣");
                    return;
                }

                StringBuffer stringBuffer = new StringBuffer();
                int i =0 ;
                int len = viewModel.observableListSelect.size();
                for (MultiItemViewModel item : viewModel.observableListSelect){
                    if (i >0 && (i)<len){
                        stringBuffer.append("/");
                    }
                    stringBuffer.append (((UserUpdateAHItemVM)item).title.get());
                    i++;

                }

                Messenger.getDefault().send(stringBuffer.toString(),Config.MGER_USER_EDIT_AH);
                getActivity().finish();


            }
        });



      String ah=  getArguments().getString("ah");
      if (!ah.equals("")){
          String[] a = ah.split("/");
          viewModel.num = a.length;
          viewModel.len.set(viewModel.num+"/3");
          for (MultiItemViewModel vm:viewModel.observableList){
              String title = ((UserUpdateAHItemVM)vm).title.get();

              if (ah.contains(title)){
                  KLog.d("title:"+title+"  ah:"+ah);
                  ((UserUpdateAHItemVM)vm).isSelect.set(true);
                  viewModel.observableListSelect.add(vm);
              }
          }
          adapter.notifyDataSetChanged();
      }

    }


}
